package com.xbongbong.outerlink;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormatDataHelp;
import com.xbongbong.paas.outerlink.AbstractPaasOuterLinkService;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.OuterLinkBaseDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.vo.AfterSaveVO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
import com.xbongbong.paas.service.ProModelService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.enums.business.SupplierCommunicateEnum;
import com.xbongbong.saas.help.SaasSaveHelp;
import com.xbongbong.saas.help.SaasUpdateHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * pro项目的抽象外链类（pro的都继承此类）
 * @author zheng.li
 **/
public abstract class AbstractProOuterLinkService extends AbstractPaasOuterLinkService {

    @Resource
    private FormatDataHelp formatDataHelp;

    @Resource
    private SaasUpdateHelp saasUpdateHelp;

    @Resource
    private SaasSaveHelp saasSaveHelp;

    @Resource
    private ProModelService proModelService;


    @Override
    public void formatData(OuterLinkBaseDTO outerLinkBaseDTO, Map<String, FieldAttrEntity> explainMap, PaasFormDataEntityExt formData, FormDataUpdateGetVO formDataUpdateGetVO) throws XbbException {
        JSONObject data = formData.getData();
        Integer businessType = outerLinkBaseDTO.getBusinessType();
        Integer saasMark = Objects.equals(businessType,0) ?  SaasMarkEnum.PAAS.getCode() : SaasMarkEnum.SAAS.getCode();
        if (Objects.equals(saasMark, SaasMarkEnum.SAAS.getCode())) {
            SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
            BeanUtil.copyProperties(outerLinkBaseDTO, saasAnalysisDataDTO);
            // saas特殊格式转换
            saasAnalysisDataDTO.setExplainMap(explainMap);
            saasAnalysisDataDTO.setBusinessType(businessType);
            saasAnalysisDataDTO.setFromOuterLink(BasicConstant.ONE);
            formatDataHelp.formatData(saasAnalysisDataDTO, data);

            // 可以放到子类实现
            if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode())) {
                if (data.containsKey(CustomerCommunicateEnum.AT_USERNAME.getAttr())) {
                    JSONArray jsonArray = data.getJSONArray(CustomerCommunicateEnum.AT_USERNAME.getAttr());
                    if (jsonArray.size() <= 0) {
                        data.remove(CustomerCommunicateEnum.AT_USERNAME.getAttr());
                    }
                }
            } else if (data.containsKey(SupplierCommunicateEnum.AT_USERNAME.getAttr())) {
                if (data.containsKey(SupplierCommunicateEnum.AT_USERNAME.getAttr())) {
                    JSONArray jsonArray = data.getJSONArray(SupplierCommunicateEnum.AT_USERNAME.getAttr());
                    if (jsonArray.size() <= 0) {
                        data.remove(SupplierCommunicateEnum.AT_USERNAME.getAttr());
                    }
                }
            }

            // saas特殊返回字段，放在一个对象中
            saasUpdateHelp.formatSaasAttrToReturn(formDataUpdateGetVO, businessType, data);
        }

        data.put(FieldTypeEnum.SERIALNO.getAlias(), formData.getSerialNo());
        data.put(FieldTypeEnum.ADDTIME.getAlias(), formData.getAddTime());
        data.put(FieldTypeEnum.UPDATETIME.getAlias(), formData.getUpdateTime());
    }


    @Override
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        saasSaveHelp.beforeSave(validateDataDTO);
    }

    @Override
    public AfterSaveVO insert(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        return proModelService.insert(formDataAddDTO, paasFormDataEntity, paasFormEntityExt);
    }

}
